<?php

namespace Common\Controller;

use Think\Controller\RestController;

class ApiController extends RestController {

    protected $allowMethod = array('get', 'post', 'put', 'delete'); // REST允许的请求类型列表
    protected $defaultMethod = 'get'; //REST默认请求类型
    protected $allowType = array('json'); // REST允许请求的资源类型列表
    protected $defaultType = 'json'; //REST默认的资源类型

    public function __construct() {
        
    }

    /**
     * 获取用户扩展信息
     * @return boolean|array false为未登录
     */
    public function getUserExInfo($user_id = '') {
        if (!is_numeric($user_id)) {
            $user_info = $this->getUserInfo();
            $user_id = $user_info['user_id'];
        }
        return D('User')->find($user_id);
    }

    /**
     * 获取用户信息
     * @return boolean|array false为未登录
     */
    public function getUserInfo() {
        $token = trim($_REQUEST['token']);
        $aes = new \Common\Utils\aes\Aes();
        $token = $aes->passport_decrypt($token, C('TOKEN_KEY'));

        if (emptys($token))
            return false;
        list($userid, $mobile, $deadline) = explode('-', $token);
        $deadline+=9990000; //保证token不过期方便测试
        if (!is_numeric($userid) || !is_numeric($mobile) || time() > $deadline) {
            return false;
        }
        return array('user_id' => $userid, 'mobile' => $mobile);
    }

    /**
     * @name  用户验证
     */
    public function auth() {
        $token = I('token');
        $aes = new \Common\Utils\aes\Aes();
        $token = $aes->passport_decrypt($token, C('TOKEN_KEY'));
        list($userid, $mobile, $deadline) = explode('-', $token);
        
        $deadline+=9990000; //保证token不过期方便测试
        if (time() > $deadline) {
            $this->error('请先登录后再进行操作...', 4);
        }
        return array('user_id' => $userid, 'mobile' => $mobile);
    }

    /**
     * @name  输出函数
     * @description 重写response方法输出json
     * @param array $data
     */
    public function response($data = array()) {

        $callback = I('callback');
        if (!emptys($callback)) {
            $this->response_jsonp($callback, $data);
        }
        parent::response($data, 'json');
    }

    public function response_jsonp($callback, $data) {
        header('Content-Type: text/javascript');
        $json = json_encode($data);
        exit("$callback($json)");
    }

    /**
     * @name 输出函数 (成功)
     * @param string $msg ,int $code
     */
    public function success($msg = "OK", $code = 1) {
        $data = array('status' => array('msg' => $msg, 'code' => $code), 'data' => new \stdClass());
        $this->response($data);
    }

    /**
     * @name 输出函数 (错误)
     * @param string $msg ,int $code
     */
    public function error($msg = null, $code = 0) {
        $data = array('status' => array('msg' => (string) $msg, 'code' => $code), 'data' => new \stdClass());
        $this->response($data);
    }

    /**
     * @name 输出函数 (数据)
     * @param string $msg ,array $data,int $code
     */
    public function output($data = array(), $msg = "OK", $code = 1) {

        $datas = array('status' => array('msg' => $msg, 'code' => $code), 'data' => (array) $data);
        $this->response($datas);
    }

    public function getCityId() {

        $city_name = trim($_REQUEST['city_name']);
        $cache_key = md5('AREA_CITY_NAME_' . $city_name);
        $city_id = S($cache_key);
        if (!is_numeric($city_id)) {
            $city_id = '510100'; //成都市
            $area_info = M('Area')->field('id')->where(array('areaname' => $city_name))->find();
            if (is_numeric($area_info['id'])) {
                $city_id = $area_info['id'];
                S($cache_key, $city_id, 86400);
            }
        }
        return $city_id;
    }

}
